//
//  HEGoodsModel.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/20.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEGoodsModel.h"
#import <RZColorful/RZColorful.h>

@implementation HEGoodsModel

- (instancetype)init {
    if (self = [super init]) {
        [HEGoodsModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{ @"desc" : @"description" };
        }];
    }

    return self;
}

+ (NSDictionary *)mj_objectClassInArray{
    return @{@"tagList" : [HEGoodsTagModel class]};
}

- (NSString *)kScaleStr {
    if (!_kScaleStr) {
        _kScaleStr = [NSString stringWithFormat:@"销量 %ld", (long)_sale];
    }
    return _kScaleStr;
}

- (NSArray *)kalbumPics {
    if (!_kalbumPics) {
        if (_albumPics != nil) {
            _kalbumPics = [_albumPics componentsSeparatedByString:@","];
        }
    }
    return _kalbumPics;
}

- (NSString *)Kprice {
    if (!_Kprice) {
        _Kprice = [NSDecimalNumber rounding:self.price afterPoint:2];
    }
    return _Kprice;
}

- (NSMutableAttributedString *)KdetailMobileHtml {
    if (!_KdetailMobileHtml) {
        
        NSMutableAttributedString *attr = [NSAttributedString htmlString: _detailMobileHtml].mutableCopy;

        // 图片的尺寸，在转换保存的时候，是保存的图片本身的size（px），在转换为富文本的时候，并没有转为pt，所以会有可能变大并别裁剪，这里做一个尺寸适配
        NSArray <RZAttributedStringInfo *> *array = [attr rz_attributedStringByAttributeName:NSAttachmentAttributeName];
        [array enumerateObjectsUsingBlock:^(RZAttributedStringInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSTextAttachment *att = obj.value;
            UIImage *image = att.image;
            if (image == nil) {
                image = [UIImage imageWithData:att.fileWrapper.regularFileContents];
            }
            CGFloat width = MIN(image.size.width, KScreenWidth - 20);
            CGFloat height = MIN(image.size.height, image.size.height * width / image.size.width);
            CGRect bounds = att.bounds;
            bounds.size = CGSizeMake(width, height);
            NSAttributedString *temp = [NSAttributedString rz_colorfulConfer:^(RZColorfulConferrer *confer) {
                confer.image(image).bounds(bounds);
            }];
            [attr replaceCharactersInRange:obj.range withAttributedString:temp];
        }];
        [attr addAttributes:@{NSFontAttributeName: DYFont(15)} range:NSMakeRange(0, attr.length)];

        
//        NSMutableAttributedString *htmlString = [[NSMutableAttributedString alloc] initWithData:[self.detailMobileHtml dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];;
        _KdetailMobileHtml = attr;
    }
    return _KdetailMobileHtml;
}

- (NSMutableAttributedString *)KOrangeprice {
    if (!_KOrangeprice) {
            //中划线
        NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        _KOrangeprice = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%@",[NSDecimalNumber rounding:self.originalPrice afterPoint:2]] attributes:attribtDic];
    }
    return _KOrangeprice;
}

@end
